home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C03 / Rotation.cpp < prev    next >
Encoding:
Text File  |  2000-05-25  |  759 b   |  32 lines

  1. //: C03:Rotation.cpp {O}
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Perform left and right rotations
  7.  
  8. unsigned char rol(unsigned char val) {
  9.   int highbit;
  10.   if(val & 0x80) // 0x80 is the high bit only
  11.     highbit = 1;
  12.   else
  13.     highbit = 0;
  14.   // Left shift (bottom bit becomes 0):
  15.   val <<= 1;
  16.   // Rotate the high bit onto the bottom:
  17.   val |= highbit;
  18.   return val;
  19. }
  20.  
  21. unsigned char ror(unsigned char val) {
  22.   int lowbit;
  23.   if(val & 1) // Check the low bit
  24.     lowbit = 1;
  25.   else
  26.     lowbit = 0;
  27.   val >>= 1; // Right shift by one position
  28.   // Rotate the low bit onto the top:
  29.   val |= (lowbit << 7);
  30.   return val;
  31. } ///:~
  32.